package com.sunther.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunther.document.dao.DictDao;
import com.sunther.document.entity.DictEntity;
import com.sunther.document.service.DictService;
import com.sunther.idb.common.util.WebResult;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author csb
 * @description: TODO
 * @date 2023/5/30 15:20
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictDao, DictEntity> implements DictService {


    @Override
    public WebResult findByType(String type) {
        QueryWrapper<DictEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type",type);
        wrapper.orderByAsc("order_by");
        List<DictEntity> list = list(wrapper);
        return WebResult.success(list);
    }

    @Override
    public Map<Integer, String> getMapByType(String type) {
        QueryWrapper<DictEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type",type);
        List<DictEntity> list = list(wrapper);
        Map<Integer, String> map = list.stream().collect(Collectors.toMap(DictEntity::getDictCode, DictEntity::getDictValue));
        return map;
    }

    /**
     * k ：value  v: code
     * @param type
     * @return
     */
    @Override
    public Map<String, Integer> getMapNameByType(String type) {
        QueryWrapper<DictEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type",type);
        List<DictEntity> list = list(wrapper);
        Map<String, Integer> map = list.stream().collect(Collectors.toMap(DictEntity::getDictValue, DictEntity::getDictCode));
        return map;
    }
}
